package ex1.game.players;

import ex1.game.Action;
import ex1.game.Player;
import ex1.game.PrisonersDilemmaGame;
import ex1.utils.Utils;

public class PavlovPlayer implements Player {

	
	private Action _myLastAction = null;
	
	@Override
	public Action playGame(PrisonersDilemmaGame game,
			Action otherPlayerLastAction) {
		
		Action action;
		if (otherPlayerLastAction == null)
		{	
			if (Utils.RandomGenerator().nextBoolean())
			{
				action = Action.Cooperate;
			}
			else
			{
				action = Action.Deffect;
			}
			_myLastAction = action;
			return action;
		}
		
		if (_myLastAction == Action.Deffect)
		{
			if (otherPlayerLastAction == Action.Cooperate)
			{
				_myLastAction = Action.Deffect;
				return Action.Deffect;
			}
			else
			{
				_myLastAction = Action.Cooperate;
				return Action.Cooperate;
			}
		}
		else //I cooperated
		{
			if (otherPlayerLastAction == Action.Cooperate)
			{
				_myLastAction = Action.Cooperate;
				return Action.Cooperate;
			}
			else //he defected
			{
				_myLastAction = Action.Deffect;
				return Action.Deffect;
			}
		}

	}
	
	@Override
	public String toString()
	{
		return "PavlovPlayer";
	}

}
